﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using TireService.Data;

namespace TireService.Website
{
    public partial class Kontakt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Button function that sends email to us with request from user to be contacted
        /// </summary>
        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            try
            {
                string emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddressContact"]);
                if (ddlArea.Value.ToString() == "0")
                {
                    emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddress"]);
                }
                if (ddlArea.Value.ToString() == "1")
                {
                    emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddressSales"]);
                }
                if (ddlArea.Value.ToString() == "2")
                {
                    emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddressFinance"]);
                }
                if (ddlArea.Value.ToString() == "3")
                {
                    emailaddress = Convert.ToString(ConfigurationManager.AppSettings["EmailAddressContact"]);
                }

                bool test = Convert.ToBoolean(Convert.ToByte(ConfigurationManager.AppSettings["TestEnvironmentOn"]));
                string message = String.Format("Kunde sendt forespørgsel via website<br><br>Navn: {0}<br>Email: {1}<br>Telefon: {2}<br>Område: {3}<br><br>Kommentar: {4}", txtName.Text, txtEmail.Text, txtPhone.Text, ddlArea.Text, txtComment.Text);

                Shared email = new Shared();
                email.SendEmail(emailaddress, txtEmail.Text, message, "Kontakt forespørgsel", true, test, null, null, null);

                // Clear form
                txtEmail.Text = "";
                txtName.Text = "";
                txtPhone.Text = "";
                txtComment.Text = "";
                
                // Show message to user
                lMessage.Text = "Tak for din forespørgsel - Vi bestræber os på at melde tilbage indenfor 24 timer.";
            }
            catch (Exception ex)
            {
                if ((ex) is ArgumentException)
                {
                    // User error - arguments not valid - show error message
                    lMessage.Text = ((ArgumentException)ex).Message;
                }
                else
                {
                    // System error - show general error message
                    lMessage.Text = "Fejl opstod - venligst prøv igen";
                }
            }
        }
    }
}